package cn.lanqiao.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.lanqiao.entity.Fill_question;
import cn.lanqiao.entity.Judge_question;
import cn.lanqiao.entity.Multi_question;
import cn.lanqiao.entity.Pages;
import cn.lanqiao.entity.Paper_manage;
import cn.lanqiao.service.Paper_manageService;
/**
 * 题库管理模块
 * @author yejun
 */
@Service
public class Paper_manageServiceImpl implements Paper_manageService {

	@Autowired
	private cn.lanqiao.mapper.Paper_manageMapper paper_manageMapper;
	@Autowired
	private cn.lanqiao.mapper.Multi_questionMapper multi_questionMapper;
	@Autowired
	private cn.lanqiao.mapper.Judge_questionMapper judge_questionMapper;
	@Autowired
	private cn.lanqiao.mapper.Fill_questionMapper fill_questionMapper;
	

	//展示所有数据
	@Override
	public List<Paper_manage> getAllPaper() {
		return paper_manageMapper.listAllPaper();
	}

	//分页
	@Override
	public Pages<Paper_manage> PaperPage(int indexPage, int pageSize, String searchkey) {
		Pages<Paper_manage> page = new Pages<Paper_manage>();
		//当前页面indexPage
		page.setIndexPage(indexPage);
		//总记录数totalData
		int totalData = paper_manageMapper.count(searchkey);
		page.setTotalData(totalData);
		//当前页面记录数pageSize;
		page.setPageSize(pageSize);
		//总页数totalPages
		int totalPages = totalData % pageSize ==0 ? totalData / pageSize : totalData/ pageSize + 1;
		page.setTotalPages(totalPages);
		
		Map<String,Object> map = new HashMap<String,Object>();
		int startindex = (indexPage -1) * pageSize;
		int endindex = pageSize;
		
		map.put("startindex", startindex);
		map.put("endindex", endindex);
		map.put("searchkey", searchkey);
		
		List<Paper_manage> data = paper_manageMapper.PaperPages(map);
		page.setPageData(data);
		
		return page;
	}
	
	//查看详情
	@Override
	public Map<String, Object> getPaperById(Integer paperId) {
		Map<String,Object>  map = new HashMap<String,Object>();
		//试卷信息
		Paper_manage paper_manage =paper_manageMapper.getPaper(paperId);
		//选择题信息
		List<Multi_question> multi_question = multi_questionMapper.getMulti(paperId);
		//判断题信息
		List<Judge_question> judge_question = judge_questionMapper.getJudge(paperId);
		//填空题信息
		List<Fill_question> fill_question = fill_questionMapper.getFill(paperId);
		map.put("paper_manage", paper_manage);
		map.put("multi_question", multi_question);
		map.put("judge_question", judge_question);
		map.put("fill_question", fill_question);
		return map;
	}
	
	//删除
	@Override
	public Map<String,Object> deletePaper(Integer paperId) {
		Map<String,Object>  map = new HashMap<String,Object>();
		//试卷
		int result1 = paper_manageMapper.delete(paperId);
		if(result1>0) {
			map.put("paperStatus", "删除试卷成功");
			//选择题
			int result2 = multi_questionMapper.delete(paperId);
			//判断题
			int result3 = judge_questionMapper.delete(paperId);
			//填空题
			int result4 = fill_questionMapper.delete(paperId);
			if(result2>0) {
				map.put("multiStatus", "清空选择题成功");
			}else {
				map.put("multiStatus", "清空选择题失败");
			}
			if(result3>0) {
				map.put("judge_questionMapper", "清空判断题成功");
			}else {
				map.put("judge_questionMapper", "清空判断题失败");
			}
			if(result4>0) {
				map.put("fill_questionMapper", "清空填空题成功");
			}else {
				map.put("fill_questionMapper", "清空填空题失败");
			}
		}else {
			map.put("paperStatus", "删除试卷失败");
		}
		return map;
	}

	//添加
	@Override
	public int insertPaper(Paper_manage paper) {
		return paper_manageMapper.insert(paper);
	}

	//查看添加详情
	@Override
	public Paper_manage insertDetailed(Paper_manage paper) {
		return paper_manageMapper.getInsertPaper(paper);
	}

	//根据试卷科目获取试卷信息
	@Override
	public List<Paper_manage> getAllBySubject(String subject) {
		return paper_manageMapper.listBySubject(subject);
	}
	
	
	//修改
//	@Override
//	public int updateStudent(Student student) {
//		return studentMapper.update(student);
//	}
	
	
}
